package com.itboye.app.service.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.login.ui.LoginActivity;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianShouZengFang;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianYouKe;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianZengSongFang;
import com.itboye.mengwu.data.model.WodeZengSongShangPingBean;

/**
 * 数据适配器
 * 
 * @author Administrator
 * 
 */
public class ItemAdapterOne extends BaseAdapter {

	WodeZengSongShangPingBean wodeZengSongShangPingBean;
	private Context mContext;

	private String footerviewItem;

	private FooterViewtwo footerView;

	// private boolean footerViewEnable = false;
	private OnClickListener ml;

	private int gridlength;

	OnClickListener imgOnClickListener;

	String zengsongrenId;
	String shouzengrenId;
	String uid;

	public ItemAdapterOne(Context context,
			WodeZengSongShangPingBean wodeZengSongShangPingBean,
			int gridlength, OnClickListener imgOnClickListener) {
		if (wodeZengSongShangPingBean != null) {
			this.wodeZengSongShangPingBean = wodeZengSongShangPingBean;
		}
		this.mContext = context;
		this.gridlength = gridlength;
		this.imgOnClickListener = imgOnClickListener;
		notifyDataSetChanged();

	}

	public void upDate(WodeZengSongShangPingBean wodeZengSongShangPingBean,
			int gridlength) {
		if (wodeZengSongShangPingBean != null) {
			this.wodeZengSongShangPingBean = wodeZengSongShangPingBean;
		}
		this.gridlength = gridlength;
		notifyDataSetChanged();
	}

	// public boolean isFooterViewEnable() {
	// return footerViewEnable;
	// }

	/**
	 * 存放列表项控件句柄
	 */
	public static class ViewHolder {

		public TextView itemview, label, content, peopleName, favNum,
				yizengchuTv;
		public ImageView bigImgInfo, peopleImg, mamazengxin;
		public LinearLayout line;

	}

	// public void setFootreViewEnable(boolean enable) {
	// footerViewEnable = enable;
	// }

	public void setOnFooterViewClickListener(OnClickListener l) {
		ml = l;
	}

	private int getDisplayWidth(Activity activity) {
		Display display = activity.getWindowManager().getDefaultDisplay();
		int width = display.getWidth();
		return width;
	}

	@Override
	public View getView(final int i, View convertView, ViewGroup parent) {
		// 伪造的空项可以根据楼盘id来确定。
		// if (footerViewEnable && i == tieZiBaoHanReply.getList().size() - 1) {
		// if (footerView == null) {
		// footerView = new FooterViewtwo(parent.getContext());
		//
		// GridView.LayoutParams pl = new
		// GridView.LayoutParams(getDisplayWidth((Activity) mContext),
		// LayoutParams.WRAP_CONTENT);
		// footerView.setLayoutParams(pl);
		// footerView.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// if (ml != null) {
		// ml.onClick(v);
		// }
		//
		// }
		// });
		// }
		// setFooterViewStatus(FooterViewtwo.MORE);
		// return footerView;
		// }
		final ViewHolder holder;

		if (convertView == null
				|| (convertView != null && convertView == footerView)) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.zengsong_list_item, parent, false);
			holder = new ViewHolder();
			// holder.itemview = (TextView) convertView
			// .findViewById(R.id.textview);
			holder.bigImgInfo = (ImageView) convertView
					.findViewById(R.id.bigImgInfo);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			// holder.label = (TextView) convertView.findViewById(R.id.label);
			holder.peopleImg = (ImageView) convertView
					.findViewById(R.id.peopleImg);
			holder.peopleName = (TextView) convertView
					.findViewById(R.id.peopleName);
			holder.favNum = (TextView) convertView.findViewById(R.id.favNum);
//			holder.mamazengxin = (ImageView) convertView
//					.findViewById(R.id.mamazengxin);
			holder.line = (LinearLayout) convertView.findViewById(R.id.line);
			holder.yizengchuTv = (TextView) convertView
					.findViewById(R.id.yizengchuTv);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		wodeZengSongShangPingBean.getList();
		// holder.itemview.setText("item" + mList.get(i));
		// holder.content.setText("\b"+"\b"+"\b"+"\b"+"\b"+"\b"+"\b"+"\b"+"\b"+"\b"+"\b"+"\b"+"\b"+"\b"+""+tieZiBaoHanReply.getList().get(i).getMessage());
		holder.content.setText((Html.fromHtml("<font color=#FF6EA5>" + "#"
				+ wodeZengSongShangPingBean.getList().get(i).getCateName()
				+ "#" + "  " + "</font>" + "<font color=#000000>"
				+ wodeZengSongShangPingBean.getList().get(i).getDetail()
				+ "</font>")));

		holder.peopleName.setText(wodeZengSongShangPingBean.getList().get(i)
				.getNickname());
		holder.favNum.setText(wodeZengSongShangPingBean.getList().get(i)
				.getApplies());

		if (wodeZengSongShangPingBean.getList().get(i).getWinUid() == null
				|| "null".equals(wodeZengSongShangPingBean.getList().get(i)
						.getWinUid()
						+ "")
				|| "".equals(wodeZengSongShangPingBean.getList().get(i)
						.getWinUid()
						+ "")) {

			holder.yizengchuTv.setVisibility(View.GONE);
		} else {
			holder.yizengchuTv.setVisibility(View.VISIBLE);
		}

		// holder.label.setText(wodeZengSongShangPingBean.getList().get(i).getCateName());
		holder.line.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 发布商品的人的uid
				zengsongrenId = wodeZengSongShangPingBean.getList().get(i)
						.getUid();
				// 获奖的人的uid
				shouzengrenId = wodeZengSongShangPingBean.getList().get(i)
						.getWinUid();
				// 当前浏览的人的uid
				uid = SPUtils.get(mContext, null, SpPublicParams.UID, "") + "";
				Log.e("!!!!1", wodeZengSongShangPingBean.getList().get(i)
						.getUid()
						+ "aaa");
				Log.e("!!!!1", wodeZengSongShangPingBean.getList().get(i)
						.getWinUid()
						+ "aaa");
				Log.e("!!!!1",
						SPUtils.get(mContext, null, SpPublicParams.UID, "")
								+ "aaa");
				Log.e("!!!!pid", wodeZengSongShangPingBean.getList().get(i)
						.getId()
						+ "aaa");

				if ("".equals(uid) || uid == null) {
					Intent intent = new Intent(mContext, LoginActivity.class);

					mContext.startActivity(intent);
					return;
				}

				if (uid.equals(zengsongrenId)) {
					Intent intent = new Intent(mContext,
							ActivityZengWuXianZengSongFang.class);
					intent.putExtra("pid", wodeZengSongShangPingBean.getList()
							.get(i).getId());
					mContext.startActivity(intent);
				} else if (uid.equals(shouzengrenId)) {
					Intent intent = new Intent(mContext,
							ActivityZengWuXianShouZengFang.class);
					intent.putExtra("pid", wodeZengSongShangPingBean.getList()
							.get(i).getId());
					mContext.startActivity(intent);
				} else {
					Intent intent = new Intent(mContext,
							ActivityZengWuXianYouKe.class);
					intent.putExtra("pid", wodeZengSongShangPingBean.getList()
							.get(i).getId());
					mContext.startActivity(intent);
				}
			}
		});
		// if ("1".equals(tieZiBaoHanReply.getList().get(i).getIsLike())) {
		// holder.mamazengxin.setSelected(true);
		// }else if ("0".equals(tieZiBaoHanReply.getList().get(i).getIsLike()))
		// {
		// holder.mamazengxin.setSelected(false);
		// }

		// holder.mamazengxin.setTag(R.id.tag_first,
		// tieZiBaoHanReply.getList().get(i).getTid());
		// holder.mamazengxin.setTag(R.id.tag_second,
		// tieZiBaoHanReply.getList().get(i).getIsLike());
		// holder.mamazengxin.setTag(R.id.tag_third, i);
		// holder.mamazengxin.setOnClickListener(imgOnClickListener);

		if (wodeZengSongShangPingBean.getList().get(i).getHead() == null
				|| "".equals(wodeZengSongShangPingBean.getList().get(i)
						.getHead())) {
			if ("1".equals(wodeZengSongShangPingBean.getList().get(i).getSex())) {
				holder.peopleImg.setImageResource(R.drawable.male);
			} else {
				holder.peopleImg.setImageResource(R.drawable.female);
			}

		} else {
			if (wodeZengSongShangPingBean.getList().get(i).getHead()
					.startsWith("http")) {
				XImageLoader.load(wodeZengSongShangPingBean.getList().get(i)
						.getHead(), holder.peopleImg);
			} else {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ wodeZengSongShangPingBean.getList().get(i).getHead(),
						holder.peopleImg);
			}
		}

		holder.bigImgInfo.setImageBitmap(null);
		if (wodeZengSongShangPingBean.getList().get(i).getShowImgs().size() > 0) {
			XImageLoader.load(GlobalConfig.API_URL
					+ "/picture/index?id="
					+ wodeZengSongShangPingBean.getList().get(i).getShowImgs()
							.get(0), holder.bigImgInfo);
		}

		return convertView;
	}

	// public FooterViewtwo getFooterView() {
	// return footerView;
	// }

	// public void setFooterViewStatus(int status) {
	// if (footerView != null) {
	// footerView.setStatus(status);
	// }
	// }

	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		if (wodeZengSongShangPingBean != null) {
			if (wodeZengSongShangPingBean.getList() != null) {
				if (wodeZengSongShangPingBean.getList().size() < gridlength) {

					return wodeZengSongShangPingBean.getList().size();
				} else {

					return gridlength;
				}
			} else {
				System.out.println(">>0" + "");
				return 0;
			}
		} else {
			return 0;

		}

		// gridlength
		// return tieZiBaoHanReply.getList().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

}
